<?php


/**
 * @author Admin
 *
 *
 */
class Images extends CI_Controller {
	//TODO - Insert your code here

	public function serve( $id,$dir_prefix='thumbs_',$width =50,$square = 0 )
	{
		parent::__construct();
		$this->load->helper(array('path','file','Image_helper'));
		$this->load->model('GalleryModel','gallerymodel');
		$img = $this->gallerymodel->getImage( $id );
		$ctype = $this->GetCtype($img->location);
		if($square == 1){
			$imgThumb =new Image($img->location,$width, $img->width,$width,"/$dir_prefix",TRUE);
		}
		else {
			$imgThumb =new Image($img->location,$width, $img->width,$img->height,"/$dir_prefix");
		}
		$imgThumb->finalize();
		header("Content-type: $ctype");
		echo file_get_contents( realpath( $imgThumb->finalPath ) );
	}
		
	protected function GetCtype( $path ){
		$file_extension = strtolower(substr(strrchr($path,"."),1));
		switch( $file_extension ) {
		    case "gif": $ctype="image/gif"; break;
		    case "png": $ctype="image/png"; break;
		    case "jpeg":
		    case "jpg": $ctype="image/jpg"; break;
		    default:
		}
		return $ctype;
	}
	/***
	 * Serve a file from the upload directory
	 */	
	public function UploadImage( $path,$dir_prefix='previews_',$_reqwidth=0 )
	{
		parent::__construct();
		$uploadDir = $this->config->item( 'uploadPath' );
		$this->load->helper( array('path','file','Image_helper') );
		$this->outputFile( "$uploadDir/$path",@getimagesize( realpath("$uploadDir/$path") ),$dir_prefix,$_reqwidth );
	}
	/***
	 * Output the file to the browser
	 */	
	protected function outputFile( $location,$imagdat,$dir_prefix,$req_width ){
		$imgThumb =new Image($location,$req_width,$imagdat[ 0 ],$imagdat[ 1 ],"/$dir_prefix");
		$imgThumb->finalize();
		$ctype = $this->GetCtype( $location );
		header( "Content-type: ".$imagdat["mime"] );
		echo file_get_contents( realpath( $imgThumb->finalPath ) );
	}
}


?>